package pc

import (
	. "chess_platform/internal/common"
	"fmt"
	"github.com/astaxie/beego/orm"
)

/*-----------------------------大区区域-----------------------------*/
//新增大区
func NewGroup(data map[string]interface{}, multiOrm ...orm.Ormer) (int64, error) {
	o := NewOrm(multiOrm, DBChessCenter)
	values, sql := InsertSql(TablePcGroup, data)
	result, err := o.Raw(sql, values).Exec()
	if err != nil {
		return 0, err
	}
	return result.LastInsertId()
}

//更新大区
func UpdateGroup(data map[string]interface{}, id int64, multiOrm ...orm.Ormer) (int64, error) {
	o := NewOrm(multiOrm, DBChessCenter)
	condition := fmt.Sprintf(`id = %v`, id)
	values, sql := UpdateSql(TablePcGroup, data, condition)
	result, err := o.Raw(sql, values).Exec()
	if err != nil {
		return 0, err
	}
	return result.RowsAffected()
}

//删除大区
func DeleteGroup(id int64, multiOrm ...orm.Ormer) (int64, error) {
	o := NewOrm(multiOrm, DBChessCenter)
	condition := fmt.Sprintf("id=%v", id)
	sql := DeleteSql(TablePcGroup, condition)
	result, err := o.Raw(sql).Exec()
	if err != nil {
		return 0, err
	}
	return result.RowsAffected()
}

//此大区是否被引用,如果已经引用则不能删除,true-绑定了不能删除,false-没绑定可以删除
func IsGroupBind(groupId int64, multiOrm ...orm.Ormer) bool {
	sql := fmt.Sprintf(`
		SELECT
			group_id
		FROM
			%v
		WHERE
			group_id = %v
	`, TableH5User, groupId)

	o := NewOrm(multiOrm, DBChessCenter)
	var gid int64
	err := o.Raw(sql).QueryRow(&gid)
	if !CheckNoExist(err) {
		return true
	}
	sql = fmt.Sprintf(`
		SELECT
			group_id
		FROM
			%v
		WHERE
			group_id = %v
	`, TablePcUser, groupId)
	err = o.Raw(sql).QueryRow(&gid)
	if !CheckNoExist(err) {
		return true
	}
	return false
}

//根据大区名字查询大区是否已经存在
func IsGroupExist(name string, multiOrm ...orm.Ormer) (int64, bool) {
	sql := fmt.Sprintf(`
		SELECT
			id
		FROM
			%v
		WHERE
			name = ?
		LIMIT 1
		`, TablePcGroup)
	o := NewOrm(multiOrm, DBChessCenter)
	var id int64
	err := o.Raw(sql, name).QueryRow(&id)
	if CheckNoExist(err) {
		return id, false
	}
	return id, true
}

//列出大区
func GroupInfoById(groupId int64, multiOrm ...orm.Ormer) (*Group, error) {
	sql := fmt.Sprintf(`
		SELECT
			*
		FROM
			%v
		WHERE
			id = %v
		LIMIT 1
		`, TablePcGroup, groupId)
	o := NewOrm(multiOrm, DBChessCenter)
	var group Group
	err := o.Raw(sql).QueryRow(&group)
	if CheckError(err) {
		return &group, err
	}
	return &group, nil
}

//列出大区
func ListGroup(p ListParams, multiOrm ...orm.Ormer) ([]Group, int64, error) {
	limit := (p.PageIndex - 1) * p.PageSize
	sql := fmt.Sprintf(`
		SELECT
			*
		FROM
			%v
		LIMIT ?,?
		`, TablePcGroup)
	o := NewOrm(multiOrm, DBChessCenter)
	var group []Group
	_, err := o.Raw(sql, limit, p.PageSize).QueryRows(&group)
	if err != nil {
		return nil, 0, err
	}
	sql = fmt.Sprintf(`SELECT COUNT(1) FROM %v `, TablePcGroup)
	var count int64
	err = o.Raw(sql).QueryRow(&count)
	return group, count, err
}

//列出大区----区域查看
func ListAreaGroup(p ListParams, multiOrm ...orm.Ormer) ([]map[string]interface{}, int64, error) {
	limit := (p.PageIndex - 1) * p.PageSize
	sql := fmt.Sprintf(`
		SELECT
			*
		FROM
			%v
		LIMIT ?,?
		`, TablePcGroup)
	o := NewOrm(multiOrm, DBChessCenter)
	var group []Group
	_, err := o.Raw(sql, limit, p.PageSize).QueryRows(&group)
	if err != nil {
		return nil, 0, err
	}
	sql = fmt.Sprintf(`SELECT COUNT(1) FROM %v WHERE group_id = ?
				AND status = %v AND level = ?`, TableH5User, FirstTopUpSuccess)
	sqlUser := fmt.Sprintf(`SELECT uid,name,real_name FROM %v WHERE group_id = ?`, TablePcUser)
	var result []map[string]interface{}
	for _, v := range group {
		var nLevel1, nLevel2 int64
		var user []orm.Params
		o.Raw(sql, v.Id, Level1).QueryRow(&nLevel1)
		o.Raw(sql, v.Id, Level2).QueryRow(&nLevel2)
		o.Raw(sqlUser, v.Id).Values(&user)
		result = append(result, map[string]interface{}{
			"id":          v.Id,
			"create_time": v.CreateTime,
			"name":        v.Name,
			"level1_num":  nLevel1,
			"level2_num":  nLevel2,
			"user":        user,
		})
	}
	sql = fmt.Sprintf(`SELECT COUNT(1) FROM %v `, TablePcGroup)
	var count int64
	err = o.Raw(sql).QueryRow(&count)
	return result, count, err
}
